package jiang.study.dadongserver;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class MainServer {
    ThreadPoolTaskExecutor executor = null;
    private boolean isStart = false;

    public void test() {
        System.out.println("test success");
    }

    public void startServer() {
        if (isStart) throw new RuntimeException("已经启动了");
        isStart = true;
        ServerSocket serverSocket = null;
        try {

            serverSocket = new ServerSocket(12345);
//            initExecutor();
//            while(true){

//            System.out.println("data=" + serverSocket.accept().getInputStream().read());
            new Thread(new MyRunnable(serverSocket.accept())).start();
//            addSocker(serverSocket.accept());
            System.out.println("进入server5");
//            }
        } catch (IOException e) {
            addSocker(null);
            e.printStackTrace();
        } finally {
            try {
                if (serverSocket != null) serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    private void addSocker(Socket socket) {
        executor.execute(new MyRunnable(socket));
    }

    private void initExecutor() {
        executor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        executor.setCorePoolSize(5);
        // 设置最大线程数
        executor.setMaxPoolSize(10);
        // 设置队列容量
        executor.setQueueCapacity(20);
        // 设置线程活跃时间（秒）
        executor.setKeepAliveSeconds(60);
        // 设置默认线程名称
        executor.setThreadNamePrefix("hello-");
        // 设置拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
    }

    public static class MyRunnable implements Runnable {
        Socket socket = null;

        JiangData beatData = new JiangData();
        public MyRunnable(Socket socket) {
            this.socket = socket;
        }
        @Override
        public void run() {
            ObjectInputStream objectInputStream = null;
            try {
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                objectOutputStream.writeObject(beatData);
                InputStream input = socket.getInputStream();
                objectInputStream = new ObjectInputStream(input);
                objectInputStream.readObject();
                int i = 0;
                while (i < 10) {
                    System.out.println("server-wait="+i);
                    if (input.available() > 0) {
                        i = 0;
                        JiangData data = (JiangData) objectInputStream.readObject();
                        System.out.println(data.type+"="+data.name);
                        objectOutputStream.writeObject(data);
                    } else {
                        i++;
                        Thread.sleep(1000);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (objectInputStream != null) {
                    try {
                        objectInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    objectInputStream = null;
                }
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                socket = null;
            }
        }
    }
}
